package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Ycl;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class YclForm extends BasePage implements Serializable {
    private GenericManager<Ycl, Long> yclManager;
    private Ycl ycl = new Ycl();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setYclManager(GenericManager<Ycl, Long> yclManager) {
        this.yclManager = yclManager;
    }

    public Ycl getYcl() {
        return ycl;
    }

    public void setYcl(Ycl ycl) {
        this.ycl = ycl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        yclManager.remove(ycl.getId());
        addMessage("ycl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            ycl = yclManager.get(id);
        } else {
            ycl = new Ycl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (ycl.getId() == null || ycl.getId() == 0);
        // set the related jbqk as a foreign key.
        ycl.setJbqk( jbqk );
        yclManager.save(ycl);

        String key = (isNew) ? "ycl.added" : "ycl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 